package com.tanhua.fenghao.service;
/*
 *FileName: FengHaoService
 *Created Date:2021/3/4 9:46
 *Developers: Mr.Seven
 */


import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.unit.DataUnit;
import com.itheima.domain.db.User;
import com.itheima.domain.db.UserFreeze;
import com.itheima.domain.db.UserInfo;
import com.itheima.domain.vo.FengHaoVo;
import com.itheima.dubbo.api.UserApi;
import com.itheima.dubbo.api.UserInfoApi;
import com.tanhua.fenghao.client.UserFreezeClient;
import com.tanhua.fenghao.domain.Result;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class FengHaoService {

    @Reference
    private UserApi userApi;
    @Reference
    private UserInfoApi userInfoApi;
    @Autowired
    private UserFreezeClient userFreezeClient;

    /**
     * Mr.Seven的基操
     * @return
     */
    public ResponseEntity findFengHao(String phone) {
        User user = userApi.findByMobile(phone);
        if (user == null) {
            return ResponseEntity.status(500).body(new Result(false, "该用户不存在"));
        }

        UserFreeze userFreeze = userFreezeClient.findByUserId(user.getId());
        if (userFreeze == null) {
            return ResponseEntity.status(500).body(new Result(false, "此用户没有被封禁"));
        }

        UserInfo userInfo = userInfoApi.findById(user.getId());
        FengHaoVo fengHaoVo = new FengHaoVo();
        if (userFreeze.getFreezingTime() != 3) {
            //限期封禁
            fengHaoVo.setNickname(userInfo.getNickname());
            Integer freezingRange = userFreeze.getFreezingRange();
            String scope = this.scopeOfTheQuery(freezingRange);
            fengHaoVo.setFreezeRange(scope);
            fengHaoVo.setReasonsForFreeze(userFreeze.getReasonsForFreezing());
            fengHaoVo.setFreezeDate(DateUtil.format(userFreeze.getFreezeDate(), "yyyy-MM-dd"));
            Integer freezingTime = userFreeze.getFreezingTime();
            fengHaoVo.setUnlockDate(this.getTheUnsealedDate(freezingTime, userFreeze.getFreezeDate()));
        } else {
            //永久封禁
            fengHaoVo.setNickname(userInfo.getNickname());
            fengHaoVo.setUnlockDate("9999-01-01");
            fengHaoVo.setFreezeRange("该用户对App无任何操作权限");
            fengHaoVo.setFreezeDate(DateUtil.format(userFreeze.getFreezeDate(), "yyyy-MM-dd"));
            fengHaoVo.setReasonsForFreeze(userFreeze.getReasonsForFreezing());
        }

        return ResponseEntity.ok(new Result(true, "查询成功", fengHaoVo));
    }

    //封禁范围
    private String scopeOfTheQuery(Integer freezingRange) {
        if (freezingRange == 1) {
            return "禁止登录";
        } else if (freezingRange == 2) {
            return "禁止发表评论";
        } else if (freezingRange == 3) {
            return "禁止发布动态";
        }
        return null;
    }

    //解封日期
    private String getTheUnsealedDate(Integer freezingTime, Date freezeDate) {
        if (freezingTime == 1) {
            //得到封禁日期三天之后解封日期
            return DateUtil.offsetDay(freezeDate, 3).toString("yyyy-MM-dd");

        } else if (freezingTime == 2) {
            //得到封禁日期七天之后解封日期
            return DateUtil.offsetDay(freezeDate, 7).toString("yyyy-MM-dd");
        }
        return null;
    }
}
